package com.rpc.server.rpc;

import com.sun.jmx.snmp.SnmpIpAddress;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

public class RpcServer {

    public void export(Object service, int port) throws IOException {
        ServerSocket server = new ServerSocket(port);
        while(true){
            final Socket socket = server.accept();
            new Thread(() -> {
                try (ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
                     ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream())){

                    String methodName = input.readUTF();
                    Class<?>[] parameterTypes = (Class<?>[])input.readObject();
                    Object[] arguments = (Object[]) input.readObject();
                    Method method = service.getClass().getMethod(methodName, parameterTypes);
                    Object result = method.invoke(service, arguments);
                    output.writeObject(result);
                } catch (IOException | ClassNotFoundException | NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
